package com.yicong.ants.ui.me;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.util.CallBacks;
import com.cchao.simplelib.util.StringHelper;
import com.yicong.ants.R;
import com.yicong.ants.databinding.PayPwdSettingsActivityBinding;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.BusinessHelper;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cch
 * @version 2020/10/6
 */
public class PayPwdSettingActivity extends BaseTitleBarActivity<PayPwdSettingsActivityBinding> implements View.OnClickListener {

    @Override
    protected int getLayout() {
        return R.layout.pay_pwd_settings_activity;
    }

    @Override
    protected void initEventAndData() {
        setTitleText("设置支付密码");
        setTitleBarVisible(false);
        AppHelper.transStatus(getWindow());

        UiHelper.setVisibleElseGone(mDataBind.verifyCodeField, true);
        mDataBind.setClick(this);

        mDataBind.apply.setEnabled(false);
        mDataBind.apply.setAlpha(0.5f);
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                setApply();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        };
        mDataBind.setPwd.addTextChangedListener(textWatcher);
        mDataBind.setPwdAgain.addTextChangedListener(textWatcher);
        mDataBind.verifyCode.addTextChangedListener(textWatcher);
    }

    private void setApply() {
        if (mDataBind.setPwd.getText().toString().length() >= 4
                && mDataBind.setPwdAgain.getText().toString().length() >= 4
                && mDataBind.verifyCode.getText().toString().length() >= 4) {
            mDataBind.apply.setEnabled(true);
            mDataBind.apply.setAlpha(1f);
        } else {
            mDataBind.apply.setEnabled(false);
            mDataBind.apply.setAlpha(0.5f);
        }
    }

    @Override
    protected void onLoadData() {
    }


    void countDownVerifyCode() {
        RxHelper.countDownConsumer(60, new CallBacks.Int() {
            @Override
            public void onCallBack(int intValue) {
                mDataBind.sendVerifyCode.setText((60 - intValue) + " s");
            }
        }, new Runnable() {
            @Override
            public void run() {
                mDataBind.sendVerifyCode.setText("获取验证码");
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.send_verify_code:
                if (!mDataBind.sendVerifyCode.getText().toString().equals("获取验证码")) {
                    return;
                }
                BusinessHelper.sendVerifyCode(this, this::countDownVerifyCode);
                break;
            case R.id.apply:
                if (StringHelper.isEmpty(mDataBind.setPwd)) {
                    UiHelper.showToast("请输入支付密码");
                    return;
                }
                if (mDataBind.setPwd.length() != 6) {
                    UiHelper.showToast("支付密码必须是6位数字");
                    return;
                }
                if (StringHelper.isEmpty(mDataBind.setPwdAgain)) {
                    UiHelper.showToast("请再次输入支付密码");
                    return;
                }
                if (!mDataBind.setPwd.getText().toString().equalsIgnoreCase(mDataBind.setPwdAgain.getText().toString())) {
                    UiHelper.showToast("两次输入的支付密码不一致");
                    return;
                }
                if (StringHelper.isEmpty(mDataBind.verifyCode)) {
                    UiHelper.showToast("请输入验证码");
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("pwd", mDataBind.setPwd.getText().toString());
                map.put("pwd_again", mDataBind.setPwdAgain.getText().toString());
                map.put("verify_code", mDataBind.verifyCode.getText().toString());

                showProgress();
                addSubscribe(RetrofitHelper.getApis().updatePayPwd(map)
                        .compose(RxHelper.toMain())
                        .subscribe(respBean -> {
                            hideProgress();
                            showToast(respBean.getMsg());
                            if (respBean.isCodeFail()) {
                                return;
                            }
                            finish();
                        }, RxHelper.getHideProgressConsumer(this)));
                break;
        }
    }
}
